package com.gr.smartedubackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gr.smartedubackend.common.BaseResponse;
import com.gr.smartedubackend.common.BusinessException;
import com.gr.smartedubackend.common.DeleteRequest;
import com.gr.smartedubackend.common.ErrorCode;
import com.gr.smartedubackend.model.dto.teaching_schedule.TeachingScheduleAddRequest;
import com.gr.smartedubackend.model.dto.teaching_schedule.TeachingScheduleQueryRequest;
import com.gr.smartedubackend.model.dto.teaching_schedule.TeachingScheduleUpdateRequest;
import com.gr.smartedubackend.model.entity.StudentGroup;
import com.gr.smartedubackend.model.entity.Teacher;
import com.gr.smartedubackend.model.entity.TeachingSchedule;
import com.gr.smartedubackend.service.StudentGroupService;
import com.gr.smartedubackend.service.TeacherService;
import com.gr.smartedubackend.service.TeachingScheduleService;
import com.gr.smartedubackend.utils.ResultUtils;
import com.gr.smartedubackend.utils.ThrowUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/teaching_schedule")
public class TeachingScheduleController {

    @Resource
    private TeachingScheduleService teachingScheduleService;

    @Resource
    private TeacherService teacherService;

    @Resource
    private StudentGroupService studentGroupService;

    @PostMapping("/add")
    public BaseResponse<Long> addTeachingSchedule(@RequestBody TeachingScheduleAddRequest teachingScheduleAddRequest) {
        ThrowUtils.throwIf(teachingScheduleAddRequest == null, ErrorCode.PARAMS_ERROR);
        Long teacherId = teachingScheduleAddRequest.getTeacherId();
        Long classId = teachingScheduleAddRequest.getClassId();
        if (teacherId == null || teacherId <= 0 || classId == null || classId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Teacher teacher = teacherService.getById(teacherId);
        if (teacher == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "教师不存在");
        }
        StudentGroup studentGroup = studentGroupService.getById(classId);
        if (studentGroup == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "班级不存在");
        }
        teachingScheduleAddRequest.setTeacherName(teacher.getTeacherName());
        teachingScheduleAddRequest.setClassName(studentGroup.getClassName());
        TeachingSchedule teachingSchedule = new TeachingSchedule();
        BeanUtils.copyProperties(teachingScheduleAddRequest, teachingSchedule);
        boolean result = teachingScheduleService.save(teachingSchedule);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(teachingSchedule.getId());
    }

    @GetMapping("/get")
    public BaseResponse<TeachingSchedule> getTeachingScheduleById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        TeachingSchedule teachingSchedule = teachingScheduleService.getById(id);
        ThrowUtils.throwIf(teachingSchedule == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(teachingSchedule);
    }

    @PostMapping("/list/page")
    public BaseResponse<Page<TeachingSchedule>> listTeachingScheduleByPage(@RequestBody TeachingScheduleQueryRequest teachingScheduleQueryRequest) {
        ThrowUtils.throwIf(teachingScheduleQueryRequest == null, ErrorCode.PARAMS_ERROR);
        int current = teachingScheduleQueryRequest.getCurrent();
        int pageSize = teachingScheduleQueryRequest.getPageSize();
        Page<TeachingSchedule> teachingSchedulePage = teachingScheduleService.page(new Page<>(current, pageSize),
                teachingScheduleService.getQueryWrapper(teachingScheduleQueryRequest));
        return ResultUtils.success(teachingSchedulePage);
    }

    @PostMapping("/list")
    public BaseResponse<List<TeachingSchedule>> listTeachingSchedule(@RequestBody TeachingScheduleQueryRequest teachingScheduleQueryRequest) {
        if (teachingScheduleQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<TeachingSchedule> teachingScheduleList = teachingScheduleService.list(teachingScheduleService.getQueryWrapper(teachingScheduleQueryRequest));
        return ResultUtils.success(teachingScheduleList);
    }


    @PostMapping("/update")
    public BaseResponse<Boolean> updateTeachingSchedule(@RequestBody TeachingScheduleUpdateRequest teachingScheduleUpdateRequest) {
        ThrowUtils.throwIf(teachingScheduleUpdateRequest == null || teachingScheduleUpdateRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        Long teacherId = teachingScheduleUpdateRequest.getTeacherId();
        Long classId = teachingScheduleUpdateRequest.getClassId();
        if (teacherId == null || teacherId <= 0 || classId == null || classId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Teacher teacher = teacherService.getById(teacherId);
        if (teacher == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "教师不存在");
        }
        StudentGroup studentGroup = studentGroupService.getById(classId);
        if (studentGroup == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "班级不存在");
        }
        teachingScheduleUpdateRequest.setTeacherName(teacher.getTeacherName());
        teachingScheduleUpdateRequest.setClassName(studentGroup.getClassName());
        TeachingSchedule teachingSchedule = new TeachingSchedule();
        BeanUtils.copyProperties(teachingScheduleUpdateRequest, teachingSchedule);
        boolean result = teachingScheduleService.updateById(teachingSchedule);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteTeachingSchedule(@RequestBody DeleteRequest deleteRequest) {
        ThrowUtils.throwIf(deleteRequest == null || deleteRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        boolean result = teachingScheduleService.removeById(deleteRequest.getId());
        return ResultUtils.success(result);
    }
}
